﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace StudentManagement
{
    public partial class frmkecheng : Form
    {
        public frmkecheng()
        {
            InitializeComponent();
        }
        private StudentInfo studentInfo;

        public StudentInfo StudentInfo
        {
            get { return studentInfo; }
            set { studentInfo = value; }
        }


        private void btnadd_Click(object sender, EventArgs e)
        {
            StudentInfo s = new StudentInfo();//弹出窗体
            s.Id = this.txtid.Text;
            s.Name = this.txtname.Text;//给属性赋值
            string connstr = "server=.;database=Class;user=sa;password=123456";
            SqlConnection conn = new SqlConnection(connstr);//执行conn的方法，开始读取
            string insertSql = "insert into Subject values('" + s.Id + "','" + s.Name + "') ";//运行脚本
            SqlCommand comm = new SqlCommand(insertSql, conn);//执行comm的方法，开始读取
            conn.Open();
            int count = comm.ExecuteNonQuery();//在数据库中几行受影响
            conn.Close();
            if (count > 0)
            {
                this.DialogResult = DialogResult.OK;//如果大于零 表示添加成功
            }
        }

        //取消
        private void btnquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        //编辑
        private void frmKecheng_Load(object sender, EventArgs e)
        {
            if (studentInfo != null)
            {
                this.txtid.Text = studentInfo.Id;
                this.txtname.Text = studentInfo.Name;//打开窗体时保存原有数据
            }
        }


    }
}
